package org.itboys.mobile.service.mysql.weixin.product;


import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.coo.conmon.Finals;
import org.itboys.coo.dto.comment.MdlCommentDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.weixin.collection.CooWeixinCollectionService;
import org.itboys.mobile.service.mongo.weixin.comment.CooWeixinCommentService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_产品_service_weixin
 */
@Service
public class SundayWeiXinProductService extends BaseMySqlService<SundayProduct> {
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private CooWeixinCollectionService collectionService;
    @Autowired
    private CooWeixinCommentService commentService;

    /**
     *分页查询商品
     * @param request
     * @return
     */
    public List<SundayProduct> selectProduct(HttpServletRequest request,Long mealId) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("mealId",mealId);
        List<SundayProduct> products=super.select(param);
 /*       if(ListUtil.isNotNull(products)){
            for(SundayProduct product:products){
                //轮播图
                Map<String,Object> imgParam = new HashMap<String, Object>();
                imgParam.put("outClassName", product.getClass().getSimpleName().toString());
                imgParam.put("outClassId", product.getId());
                List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
                product.setRollImages(rollImages);
            }
        }*/
        return products;
    }
    /**
     * 查找单个
     * @param productId
     * @return
     */
    public SundayProduct findOne(Long productId){
        SundayProduct product =super.find(productId);
        //轮播图
        Map<String,Object> imgParam = new HashMap<String, Object>();
        imgParam.put("outClassName", product.getClass().getSimpleName().toString());
        imgParam.put("outClassId", product.getId());
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        product.setRollImages(rollImages);
        return product;

    }
    public SundayProductDto packageDtoSingle(SundayProduct product, Long memberId,List<Map<String,Object>> recommendProducts){

        SundayProductDto dto = null;
        //静态资源


        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        //商品详情URL
        String descUrl =resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/desc?id="+product.getId();
        //判断受否收藏
        Integer isCollection =collectionService.checkIsCollection(memberId, Finals.collection_type_product,product.getId());
        //加载默认评论
        logger.info("加载评论");
        List<MdlCommentDto> comments = commentService.getCommentByNumber(2,product.getId(),SundayProduct.class.getSimpleName());
        logger.info("加载评论-结束");
        //加载推荐商品

        dto=  new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                product.getName()+product.getStandard(), product.getNumber(), product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),  product.getType(),
                product.getImage(),  product.getSaleNumber(),product.getCartNumber(), rollImages,
                isCollection,  descUrl,  recommendProducts, comments,product.getStore());


        return dto;
    }
}
